if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            user: get_cookie("user"),
            key: get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        linshi_search: {
            sid: "",
            pid: "",
            tid: "",
            cid: "",
            search: ""
        },
        linshi_id: null, //临时的id存储 用来判断同一个按钮的操作id
        linshi_arrid: [],
        linshi_type: null,//1 添加   2 删除
        list_ajax_page:1,
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        // 检查功能一般检查是否为空
        jiancha: function () {
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint($(".add_goods_body .input_is:visible").eq(i).prev().text() + " 没有填写！");
                    return false;
                }
            }
            return true;
        },
        // 获取商品的数组
        shop_list_ajax: function (page_number) {
            var n = page_number ? page_number : houtai.list_ajax_page=1;
            $.ajax({
                url: ip + 'index.php?s=admincms/package/show',
                type: "post",
                data: {
                    sid: $("#taocan_class1").val(),
                    pid: $("#taocan_class2").val(),
                    token: get_cookie("key"),
                    page:n
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);
                    }
                    houtai.table_string(res);

                    $("#page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.list_ajax_page=pageIndex;
                            houtai.shop_list_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        // 把获取的商品数组写入table
        table_string: function (res) {
            console.log(res);
            // 写入table
            var string_head = "<thead> <tr> <th>编号</th> <th>名称</th> <th>品牌</th><th>规格</th><th>颜色</th><th>数量</th> <th>图片</th>  <th>操作</th>   </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='8'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td class="taocan_id">' + o.id + '</td>' +
                        '<td class="taocan_name" >' + o.names + '</td>' +
                        '<td class="taocan_pinpai" >' + o.brand + '</td>' +
                        '<td class="taocan_guige" >' + o.specs + '</td>' +
                        '<td class="taocan_color">' + o.color + '</td>' +
                        '<td class="taocan_number">' + o.amount + '</td> ' +
                        '<td class="taocan_img"><img style="width:50px" src="' + o.img + '" alt=""></td> ' +
                        '<td style="width:170px;" class="taocan_control"><button type="button" class="btn btn-warning btn-xs set"> <span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 修改</button> <button type="button" class="btn btn-danger btn-xs remove"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button></td> ' +
                        '</tr>';
                });
            }

            $("#taocan_table").empty().append(string_head + string_body);
        },
        //初始类
        chushi_class: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/package/showSize",
                async: false,
                type:"post",
                data:{
                    token: get_cookie("key")
                },
                success: function (res) {
                    console.log(JSON.parse(res));
                    if (JSON.parse(res).resStatus === 0) {
                        var arr = JSON.parse(res).data;
                        for (var ii = 0, string11 = ""; ii < arr.length; ii++) {
                            string11 += '<option value="' + arr[ii].sid + '">' + arr[ii].title + '</option>';
                        }
                        $("#taocan_class1").empty().append(string11);
                        for (var aa = 0,
                                 string22 = ''; aa < arr[0].package.length; aa++) {
                            string22 += '<option value="' + arr[0].package[aa].pid + '">' + arr[0].package[aa].title + '</option>';
                        }
                        $("#taocan_class2").empty().append(string22);
                    }
                }
            });
        },
        //类型请求
        class_request_ajax: function (class_n) {
            $.ajax({
                url: ip + "index.php?s=admincms/package/showTypes",
                type: "post",
                data: {
                    pid: class_n,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    if (res.resStatus === 0) {
                        houtai.arr = res.data;
                        var class1_string = "";
                        var class2_string = "";
                        $.each(res.data, function (i, o) {
                            class1_string += '<option value="' + o.tid + '">' + o.title + '</option>';
                        });
                        $.each(res.data[0].class, function (i, o) {
                            class2_string += '<option value="' + o.cid + '">' + o.title + '</option>';
                        });
                        $("#taocan_select1").empty().append(class1_string);
                        $("#taocan_select2").empty().append(class2_string);
                    }
                }
            });

        },
        //分类商品请求
        class_shangpin_ajax: function (page_number) {
            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + 'index.php?s=admincms/package/addShow',
                type: "post",
                data: {
                    sid: houtai.sid,
                    pid: houtai.pid,
                    tid: houtai.tid,
                    cid: houtai.cid,
                    search: houtai.search,
                    page: n,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    houtai.class_shangpin_string(res);

                    $("#page_inbound").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.class_shangpin_ajax(pageIndex);
                        }
                    });
                    if (!page_number && res.resStatus === 0) {
                        houtai.hint("<span style='color:#0f0;'>为你找到" + res.totalpage + "条数据</span>");
                    }
                }

            });
        },
        // 分类商品请求写入table
        class_shangpin_string: function (res) {
            // 写入table
            var string_head = "<thead> <tr> <th>编号</th> <th>名称</th> <th>品牌</th><th>规格</th><th>数量</th><th>操作</th>   </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='6'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td class="add_taocan_id">' + o.id + '</td>' +
                        '<td class="add_taocan_name" >' + o.Names + '</td>' +
                        '<td class="add_taocan_pinpai" >' + o.brand + '</td>' +
                        '<td class="add_taocan_guige" >' + o.specs + '</td>' +
                        '<td class="add_taocan_number"><input type="number" class="form-control"></td> ' +
                        '<td style="width:200px;" class="add_taocan_control"><button type="button" class="btn btn-primary btn-xs add"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加</button></td> ' +
                        '</tr>';
                });
            }

            $("#add_taocan_table").empty().append(string_head + string_body);
        },
        //添加商品加入套餐
        tianjia: function () {
            var n = "";
            for (var i = 0; i < $("#add_taocan_table tr>.add_taocan_id").length; i++) {
                if ($("#add_taocan_table tr>.add_taocan_id").eq(i).text() === houtai.linshi_id) {
                    n = $("#add_taocan_table tr>.add_taocan_id").eq(i).parents("tr").find(".add_taocan_number>input").val();

                }
            }
            if (n !== "") {
                $.ajax({
                    url: ip + "index.php?s=admincms/package/add",
                    type: "post",
                    data: {
                        wid: houtai.linshi_id,
                        sid: $("#taocan_class1").val(),
                        pid: $("#taocan_class2").val(),
                        amount: n,
                        token: get_cookie("key")
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            for (var i = 0; i < $("#add_taocan_table tr>.add_taocan_id").length; i++) {
                                if ($("#add_taocan_table tr>.add_taocan_id").eq(i).text() === houtai.linshi_id) {
                                     $("#add_taocan_table tr>.add_taocan_id").eq(i).parents("tr").remove();
                                       break;
                                }
                            }
                            houtai.shop_list_ajax();
                            houtai.hint("<span style='color:#0f0;'>添加成功！！！</span>");
                            $("#modal_wrap").fadeOut();
                        }
                    }
                });
            }

        },
        // 从套餐删除一个商品console.log(res);
        remove: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/package/del",
                type: "post",
                data: {
                    id: houtai.linshi_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.hint("<span style='color:#0f0;'>删除成功！！！</span>");
                        for (var i = 0; i < $("#taocan_table tr>.taocan_id").length; i++) {
                            if ($("#taocan_table tr>.taocan_id").eq(i).text() === houtai.linshi_id) {
                                $("#taocan_table tr>.taocan_id").eq(i).parents("tr").remove();
                                break;
                            }
                        }
                        $("#modal_wrap").fadeOut();
                    }
                }
            });
        },
        xiugai: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/package/alter",
                type: "post",
                data: {
                    id: houtai.linshi_id,
                    amount: $("#set_count").val(),
                    token: get_cookie("key")

                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.hint("<span style='color:#0f0;'>修改成功！！！</span>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#taocan_set").fadeOut();
                    }
                }
            });
        }
    };
    houtai.chushi_class();
    houtai.class_request_ajax($("#taocan_class2").val());
    houtai.linshi_search.class1 = $("#select1").val();
    houtai.linshi_search.class2 = $("#select2").val();
    houtai.shop_list_ajax();
// 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });

    // 户型套餐下拉
    $("#taocan_class1").bind("change", function () {
        houtai.linshi_search.sid = $("#taocan_class1").val();
        houtai.linshi_search.pid = $("#taocan_class2").val();
        houtai.shop_list_ajax();
    });

    // 辅材类型下拉
    $("#taocan_class2").bind("change", function () {
        houtai.linshi_search.sid = $("#taocan_class1").val();
        houtai.linshi_search.pid = $("#taocan_class2").val();
        houtai.shop_list_ajax();
        houtai.class_request_ajax($(this).val());
    });

    // 套餐添加商品按钮
    $("#taocan_add_btn").bind("click", function () {
        $("#taocan_add").fadeIn();
        houtai.sid = $("#taocan_class1").val();
        houtai.pid = $("#taocan_class2").val();
        houtai.tid = $("#taocan_select1").val();
        houtai.cid = $("#taocan_select2").val();
        houtai.search = "";
        houtai.class_shangpin_ajax();
    });

    //一级分类下拉
    $("#taocan_select1").bind("change", function () {
        var that = this;
        $.each(houtai.arr, function (i, o) {
            if ($(that).val() == o.tid) {
                var class2_string = "";
                $.each(o.class, function (i, o) {
                    class2_string += '<option value="' + o.cid + '">' + o.title + '</option>';
                });
                $("#taocan_select2").empty().append(class2_string);
                return 0;
            }
        });
        houtai.sid = $("#taocan_class1").val();
        houtai.pid = $("#taocan_class2").val();
        houtai.tid = $("#taocan_select1").val();
        houtai.cid = $("#taocan_select2").val();
        houtai.search = "";
        houtai.class_shangpin_ajax();
    });

    //二级分类下拉
    $("#taocan_select2").bind("change", function () {
        houtai.sid = $("#taocan_class1").val();
        houtai.pid = $("#taocan_class2").val();
        houtai.tid = $("#taocan_select1").val();
        houtai.cid = $("#taocan_select2").val();
        houtai.search = "";
        houtai.class_shangpin_ajax();
    });
    // 添加一个商品加入套餐
    $("#add_taocan_table").on("click", ".add", function () {
        if ($(this).parents("tr").find(".add_taocan_number>input").val() === "0" || $(this).parents("tr").find(".add_taocan_number>input").val() === "") {
            houtai.hint("请输入加入商品的数量！！！");
            $(this).parents("tr").find(".add_taocan_number>input").focus();
        } else {
            houtai.linshi_type = 1;
            houtai.linshi_id = $(this).parents('tr').find('.add_taocan_id').text();
            $(".modal").show().find(".modal-body").empty().append("" +
                "您确定要把 <span style='color:#f10'>" +
                $(this).parents("tr").find(".add_taocan_number>input").val() + "</span>件<span style='color:#68ca65'>" +
                $(this).parents('tr').find('.add_taocan_name').text() +
                "</span>加入<span style='color:#f7b902'>" +
                $('#taocan_class1 option:selected').text() +
                "套餐</span>中的<span style='color:#0897fd'>" +
                $('#taocan_class2 option:selected').text() +
                "</span>吗？"
            );
        }

    });
    // 从套餐中删除一个商品
    $("#taocan_table").on("click", ".remove", function () {
        houtai.linshi_id = $(this).parents("tr").find(".taocan_id").text();
        houtai.linshi_type = 2;
        $("#modal_wrap").fadeIn().find(".modal-body").empty().append("" +
            "您确定要从套餐中移除 <span style='color:#f10;'>" +
            $(this).parents("tr").find(".taocan_name").text() +
            "</span>吗？"
        );
    });
    // 从套餐中修改一个商品
    $("#taocan_table").on("click", ".set", function () {
        houtai.linshi_id = $(this).parents("tr").find(".taocan_id").text();
        $("#set_bianhao").val($(this).parents("tr").find(".taocan_id").text());
        $("#set_brand").val($(this).parents("tr").find(".taocan_pinpai").text());
        $("#set_name").val($(this).parents("tr").find(".taocan_name").text());
        $("#set_count").val($(this).parents("tr").find(".taocan_number").text());
        $("#taocan_set").fadeIn();
    });
    //确认事件
    $("#confirm").bind("click", function () {
        if (houtai.linshi_type === 1) {
            if (houtai.click_yes()) {
                houtai.tianjia();
            }
        } else if (houtai.linshi_type === 2) {
            if (houtai.click_yes()) {
                houtai.remove();
            }
        }

    });
    //确认修改
    $("#set").bind("click", function () {
        if (houtai.jiancha()) {
            if (houtai.click_yes()) {
                houtai.xiugai();
            }

        }

    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(".set_shangpin").fadeOut(200);

    });
    //历史查询弹框关闭
    $("#close_table_history").bind("click", function () {
        $("#taocan_add").fadeOut(200);
    });


    //搜索库存商品
    $("#search_all").bind("click", function () {
        if ($("#search_input").val() === "") {
            houtai.hint("请输入后再查询");
            $("#search_input").focus();
        } else {
            houtai.sid = $("#taocan_class1").val();
            houtai.pid = $("#taocan_class2").val();
            houtai.tid = "";
            houtai.cid = "";
            houtai.search = $("#search_input").val();
            houtai.class_shangpin_ajax();
        }
    });
    //回车按钮操作
    $("#search_kucun_form").submit(function () {
        if ($("#search").val() === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.user_type = "";
            houtai.linshi_search.search = $("#search").val();
            houtai.shop_list_ajax();
        }
        return false;
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});
